<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Mess extends Auth_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('mess_mdl');
    }

    public function mess_count(){
        if(!$this->input->is_ajax_request()){
            show_error('参数错误！');
        }
        $this->db->where('status',1);
        $this->db->where('receive',$_SESSION['user']['id']);
        //$this->db->or_where('receive',1);


        $num = $this->mess_mdl->get_count();

        echo $num;
    }

    /**
     * 消息查看
     */
    public function index(){

        $url = '';
        $this->db->start_cache();

        $this->db->where('receive',$_SESSION['user']['id']);
        //$this->db->or_where('receive',1);

        $s = $this->input->get('s');
        if($s == 2){
            $this->db->where('status','2');
            $url = '?s='.$s;
        }else{
            $this->db->where('status','1');
        }

        $this->db->stop_cache();

        $this->load->library('pagination');

        $config['base_url'] = site_url('mess/index').$url;
        $config['total_rows'] = $this->mess_mdl->get_count();
        $config['per_page'] = 10;
        $config['page_query_string'] = TRUE;
        $config['first_link'] = '首页';
        $config['last_link'] = '尾页';
        $config['next_link'] = '下一页';
        $config['prev_link'] = '上一页';
        $config['anchor_class'] = 'current';

        $this->pagination->initialize($config);
        $data['page'] = $this->pagination->create_links();

        $per_page = $this->input->get('per_page');
        $this->db->limit($config['per_page'],intval($per_page));
        $this->db->order_by('status asc,create_time desc');
        $list = $this->mess_mdl->select();
        //echo $this->db->last_query();
        /** 将消息全部设置为已读 */
        $this->db->limit($config['per_page'],intval($per_page));
        $this->db->set('status','2');
        $this->mess_mdl->update();

        $this->db->flush_cache();

        $data['list'] = $list;
        $this->_view('index',$data);
    }

    /**
     * 消息删除
     */
    public function delete(){
        if(!$this->input->is_ajax_request()){
            show_error('参数错误！');
        }
        $mid = $this->input->post('mid');
        if(!is_numeric($mid) && $mid < 1){
            die('参数错误！');
        }

        $this->db->set('status','3');
        $this->db->where('id',$mid);
        $this->db->where('receive',$_SESSION['user']['id']);
        $res = $this->mess_mdl->update();

        echo $res == true ? 1 : '删除失败！';
    }

}

/* End of file Mess.php */
/* Location: ./application/controllers/Mess.php */